package com.boot2523.common;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @ControllerAdvice表示所有控制层进入对应方法时，先运行有模型属性的方法
 */
@ControllerAdvice
public class MyControl {

    /**
     * 控制层方法有异常时，进行全局异常处理
     * @param e
     * @return
     */
    //处理异常
    @ExceptionHandler
    public ResponseEntity<MyError> handleException(Exception e){
        MyError error=new MyError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Exception",e.getMessage());
        return new ResponseEntity<>(error,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
